#include "MainBoard.h"
#include "EventReciever.h"

MainBoard::MainBoard(float x, float y)
{
	m_pMusic1 =new MusicBoard(true,x,y);
	m_pMusic2 =new MusicBoard(false,x,y);
	m_pSample1 =new SampleBoard(100,x,y);
	m_pSample2 =new SampleBoard(200,x,y);
	m_pSample3 =new SampleBoard(300,x,y);
	m_pSample4 =new SampleBoard(400,x,y);
	m_pSample5 =new SampleBoard(500,x,y);

	m_pCrossFader = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>(40*x,456*y,600*x,456*y+16*y),0,99);
	m_pCrossFader->setToolTipText(L"Crossfader");
	m_pCrossFader->setMin(0);
	m_pCrossFader->setMax(90);
	m_pCrossFader->setSmallStep(10);
	m_pCrossFader->setPos(45);
	m_pMasterMusicVol = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>(310*x,316*y,330*x,316*y+128*y),0,98);
	m_pMasterMusicVol->setToolTipText(L"Master Music Vol.");
	m_pMasterMusicVol->setMin(-256);
	m_pMasterMusicVol->setMax(0);
	m_pMasterMusicVol->setPos(-256);
	m_pMasterEffectVol = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>(8*x,84*y,28*x,84*y+128*y),0,97);
	m_pMasterEffectVol->setToolTipText(L"Master Effect Vol. (adjusts relative)");
	m_pMasterEffectVol->setMin(-256);
	m_pMasterEffectVol->setMax(0);
	m_pMasterEffectVol->setPos(-256);
	m_pDistance = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>(32*x,84*y,40*x,84*y+128*y),0,96);
	m_pDistance->setToolTipText(L"Distance Override");
	m_pDistance->setMin(-256);
	m_pDistance->setMax(0);
	m_pDistance->setPos(0);
	m_pReverb = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>(48*x,84*y,56*x,84*y+128*y),0,95);
	m_pReverb->setToolTipText(L"Reverb Override");
	m_pReverb->setMin(-256);
	m_pReverb->setMax(0);
	m_pReverb->setPos(0);
	m_MuteEffects = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(8*x,30*y,56*x,30*y+16*y),0,94,L"MUTE EFFECTS",L"Mutes ALL Effects");
	m_MuteEffects->setIsPushButton(true);
	m_EffectOverride = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(8*x,54*y,56*x,54*y+28*y),0,93,L"OVERRIDE",L"Uses values below for all effects");
	m_EffectOverride->setIsPushButton(true);
	m_MuteMusic = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(280*x,280*y,360*x,280*y+32*y),0,92,L"MUTE MUSIC",L"Mutes ALL music tracks");
	m_MuteMusic->setIsPushButton(true);

}
void MainBoard::Update()
{
	bool musicmute =m_MuteMusic->isPressed();
	bool effectsmute =m_MuteEffects->isPressed();
	float musicvol = m_pMasterMusicVol->getPos();
	float effectsvol = m_pMasterEffectVol->getPos();
	float m = -2.56;
	musicvol /=m;
	effectsvol /=m;
	musicvol /=100;
	effectsvol /=100;
	m_pMusic1->Update(musicvol*m_Fade1,musicmute);
	m_pMusic2->Update(musicvol*m_Fade2,musicmute);
	m_pSample1->Update(effectsvol,effectsmute);
	m_pSample2->Update(effectsvol,effectsmute);
	m_pSample3->Update(effectsvol,effectsmute);
	m_pSample4->Update(effectsvol,effectsmute);
	m_pSample5->Update(effectsvol,effectsmute);

	if(m_EffectOverride->isPressed())
	{
		float dist = m_pDistance->getPos();
		float reverb = m_pReverb->getPos();
		float m=-25.6;
		dist /=m;
		reverb /=m;
		m_pSample1->Override(dist,reverb);
		m_pSample2->Override(dist,reverb);
		m_pSample3->Override(dist,reverb);
		m_pSample4->Override(dist,reverb);
		m_pSample5->Override(dist,reverb);
		
	}
	m_CrossPos=m_pCrossFader->getPos();
	if(m_CrossPos!=m_LastCrossPos)
	{
		m_LastCrossPos=m_CrossPos;
		float r = m_CrossPos*(PI/180);
		m_Fade1=cos(r);
		m_Fade2=sin(r);
	}

}
bool MainBoard::EventHandler(const SEvent& event)
{

	if (event.EventType == EET_GUI_EVENT)
	{
		s32 id = event.GUIEvent.Caller->getID();

		if(id>=0 && id<100) // this
		{
			
		}
		if(id>=100 && id<200)
		{
			return m_pSample1->EventHandler(event);
		}
		if(id>=200 && id<300)
		{
			return m_pSample2->EventHandler(event);
		}
		if(id>=300 && id<400)
		{
			return m_pSample3->EventHandler(event);
		}
		if(id>=400 && id<500)
		{
			return m_pSample4->EventHandler(event);
		}
		if(id>=500 && id<600)
		{
			return m_pSample5->EventHandler(event);
		}

		if(id>=1000 && id<2000)
		{
			return m_pMusic1->EventHandler(event);
		}
		if(id>=2000 && id<3000)
		{
			return m_pMusic2->EventHandler(event);
		}

	}
	return false;
}
void MainBoard::Cleanup()
{
	m_pMusic1->Cleanup();
	m_pMusic2->Cleanup();
	m_pSample1->Cleanup();
	m_pSample2->Cleanup();
	m_pSample3->Cleanup();
	m_pSample4->Cleanup();
	m_pSample5->Cleanup();
}
